from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from app.serve.user.auth import login_required
from app.serve.user.permission import PermissionServer
from app.util.check import split_convert_int
from app.util.exception import WtreesReturn, ErrorType
from app.serve.user.groups import GroupServe


class UserGroupApi(Resource):
    """
        用户分组相关
    """

    @login_required
    def get(self):
        data, count = GroupServe.group_all()
        return WtreesReturn.data(ErrorType.OK, data=data, page=count)

    @login_required
    def post(self):
        data = PermissionServer.group_permission()
        return WtreesReturn.data(ErrorType.OK, data=data, )


class GroupCreate(Resource):
    """
          新建分组
    """
    get_data = RequestParser()
    get_data.add_argument("id", type=str, help='角色ID')
    get_data.add_argument("name", type=str, help='角色名称', required=True)
    get_data.add_argument("info", type=str, help='角色详情', required=False)
    get_data.add_argument("ids", type=split_convert_int, help='角色权限', required=False)

    @login_required
    def post(self):
        args = self.get_data.parse_args()
        GroupServe.create_group(gid=args['id'], name=args['name'], info=args['info'], permission_ids=args['ids'])
        return WtreesReturn.data(ErrorType.OK)

    # @login_required
    # def post(self):
    #     data = PermissionServer.group_permission()
    #     return WtreesReturn.data(ErrorType.OK, data=data)
